<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products_Entity extends CI_Model
{
    private $tablename = 'products';
    private $id = null;
    private $title = null;
    private $alias = null;
    private $info = null;
    private $price = null;
    private $image = null;
    private $hit = null;
    private $hot = null;
    private $status = null;
    private $order = null;
    private $created = null;
    private $date_bg = null;
    private $date_end = null;
    private $amount = null;
    private $tid = null;
    private $meta_tag = null;
    private $meta_description = null;
    
    public function setId($Id)
    {
        $this->id = $Id;
    }

    public function setTitle($Title)
    {
        $this->title = $Title;
    }

    public function setAlias($Alias)
    {
        $this->alias = $Alias;
    }

    public function setInfo($Info)
    {
        $this->info = $Info;
    }

    public function setPrice($Price)
    {
        $this->price = $Price;
    }

    public function setImage($Image)
    {
        $this->image = $Image;
    }

    public function setHit($Hit)
    {
        $this->hit = $Hit;
    }

    public function setHot($Hot)
    {
        $this->hot = $Hot;
    }

    public function setStatus($Status)
    {
        $this->status = $Status;
    }

    public function setOrder($Order)
    {
        $this->order = $Order;
    }

    public function setCreated($Created)
    {
        $this->created = $Created;
    }

    public function setDate_bg($Date_bg)
    {
        $this->date_bg = $Date_bg;
    }

    public function setDate_end($Date_end)
    {
        $this->date_end = $Date_end;
    }

    public function setAmount($Amount)
    {
        $this->amount = $Amount;
    }

    public function setTid($Tid)
    {
        $this->tid = $Tid;
    }

    public function setMeta_tag($Meta_tag)
    {
        $this->meta_tag = $Meta_tag;
    }

    public function setMeta_description($Meta_description)
    {
        $this->meta_description = $Meta_description;
    }

    public function checkExist_product_types($tid)
    {
        $query = $this->db->get_where('product_types', array('id' => $tid));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function checkExistFK_orders_detail($id)
    {
        $query = $this->db->get_where('orders_detail', array('pid' => $id));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == 'tablename') continue;
            if ($val == null || $val == '') continue;
            $params[$key] = $val;
        }
        
        // foreign key check
        if (!isset($params['tid']) || !$this->checkExist_product_types($params['tid'])) {
            return -1;
        }        
        if (!isset($params['id'])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array('id' => $params['id']));
        }
        
    }
    
    public function del($id)
    {
        // check exist foreign key
        if ($this->checkExistFK_orders_detail($id)) {
            return array('-1', 'products'.' '.t('is exist in').' '.'orders_detail');
        }
        
        return $this->db->delete($this->tablename, array('id' => $id));    
    }
    
    public function getProductsById($id)
    {
        return $this->db->get_where($this->tablename, array('id' => $id));
    }
    
    public function getAllList()
    {
        return $this->db->get($this->tablename);
    }
    
    public function TotalProducts()
    {
        return $this->db->count_all_results('Products'); 
    } 
}       
        